<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>编辑菜单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <input type="hidden" name="id">
    <div class="layui-form-item">
        <label class="layui-form-label required">父菜单ID</label>
        <div class="layui-input-block">
            <input type="text" name="pid" lay-reqtext="父菜单ID" placeholder="父菜单ID" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">菜单名称</label>
        <div class="layui-input-block">
            <input type="text" name="title" lay-verify="required" lay-reqtext="菜单名称" placeholder="菜单名称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单图标</label>
        <div class="layui-input-block">
            <input type="text" name="icon" lay-reqtext="菜单图标" placeholder="菜单图标" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">请求路径</label>
        <div class="layui-input-block">
            <input type="text" name="href" lay-verify="required" lay-reqtext="请求路径" placeholder="请求路径" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序号</label>
        <div class="layui-input-block">
            <input type="text" name="sort" lay-verify="required" lay-reqtext="排序号" placeholder="排序号" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单类型</label>
        <div class="layui-input-block" lay-verify-msg="菜单类型" lay-verify="mustcheck">
            <input type="checkbox" name="menuFlag" title="目录" value="0">
            <input type="checkbox" name="menuFlag" title="菜单" value="1">
            <input type="checkbox" name="menuFlag" title="按钮" value="2">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <textarea name="remark" class="layui-textarea" placeholder="请输入描述"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">提交</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        //自定义验证规则
        form.verify({
            mustcheck: function (value, item) { //复选框必选
                var xname = $(item).attr("id")
                var va = $(item).find("input[type='checkbox']:checked").val();
                if (typeof (va) == "undefined") {
                    return $(item).attr("lay-verify-msg");
                }
            },
            mustRadio: function (value, item) { //单选必选
            var xname = $(item).attr("id")
            var va = $(item).find("input[type='radio']:checked").val();
            if (typeof (va) == "undefined") {
                return $(item).attr("lay-verify-msg");
            }
        },
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let val = $("input[name=id]").val();
            var params = {
                'pid': $("input[name=pid]").val(),
                'title': $("input[name=title]").val(),
                'icon': $('input[name="icon"]').val(),
                'href': $('input[name="href"]').val(),
                'sort': $('input[name="sort"]').val(),
                'menuFlag': $("input[name=menuFlag]:checked").val(),
                'remark': $('textarea[name="remark"]').val()
            };
            if(val!=null && val!='' && val!=undefined){
                params.id = val;
            }

            $.ajax({
                type: 'POST',
                beforeSend: function() {
                    layer.load(2);
                },
                url: '/menu/saveOrUpdate',
                // data: params,
                data: JSON.stringify(params),
                dataType: 'JSON',
                contentType: 'application/json; charset=UTF-8',
                success: function (res) {
                    if(res.code == '200') {
                        layer.msg(res.msg, { icon: 1 ,time: 1000},function () {
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                            //刷新父页面的table
                            parent.layui.table.reload('menuTableId');
                        });
                    }else {
                        layer.msg(res.msg, {icon: 5, time: 2000});
                    }
                }, complete: function() {
                    layer.closeAll("loading");
                },
                error: function() {
                    layer.msg('系统繁忙请稍后重试', {icon: 5, time: 2000});
                }
            });
            return false;
        });

    });
</script>
</body>
</html>